//logs.js
var util = require('../../utils/util.js')
Page({
  data: {
      item: {
          display    : 'hide',
          error_info : '请输入比赛阶段',
      },
      battle_type_list: ['', '八球', '斯诺克', '九球'],
      battle_type_name: '八球',
      button       : {disabled:false},
      event_stage  : '',
      sum_frame    : '',
      player1_name : '',
      player2_name : ''
  },
  onShow: function (option) {
    //赛事信息
    var battle_type = wx.getStorageSync('battle_type')|| 1;
    
    wx.setStorage({
      key: "battle_type",
      data: battle_type
    })

    this.setData({
      battle_type_name: this.data.battle_type_list[battle_type]
    })
  },  
  //输入框值改变触发
  changeEventStage:function(e){
      this.data.event_stage = e.detail.value;
  },
  changeSumFrame:function(e){
      this.data.sum_frame = e.detail.value;
  },
  changePlayer1Name:function(e){
      this.data.player1_name = e.detail.value;
  },
  changePlayer2Name:function(e){
      this.data.player2_name = e.detail.value;
  },
  

  //点击保存按钮触发
  doSave:function(){
      var that = this;
      //验证用户的输入
      if(this.checkInput()){
          that.setDisabled();
          
          wx.showLoading({title: '保存中...',});

          var url  = getApp().data.servser+'/Battle/add';

          wx.request({
              url: url,
              data: {
                  'event_stage' : that.data.event_stage,
                  'sum_frame'   : that.data.sum_frame,
                  'player1_name': that.data.player1_name,
                  'player2_name': that.data.player2_name,
                  'event_id'    : wx.getStorageSync('event_id'),
                  'battle_id'   : wx.getStorageSync('battle_info').battle_id,
                  'battle_type': wx.getStorageSync('battle_type')
               },
               header : {'content-type': 'application/json'},
               method : "GET",

               success: function(res) {
                   wx.hideLoading();
                   if(res.data.state == 1){
                   //将对局信息保存在本地缓存中
                       that.saveBattleInfo(res.data.id);
                       wx.showToast({
                         title: '保存成功！请点击开始记分按钮',
                         icon: 'success',
                         duration: 3000,
                         success:function(){
                           wx.navigateBack(1);
                         }
                       })
                       
                   }else{
                       that.setDisabled();
                       that.setData({
                       item: {
                          display    :'show',
                          error_info : res.data.info
                      },
                   })
                   that.closeErrorInfo();
               }
            }
          })
      }
   },
   

  updateGameType: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['八球', '斯诺克', '九球'],
      success: function (res) {
        if (res.tapIndex || res.tapIndex === 0) {
          var itemList = [1, 2, 3];
          var point = itemList[res.tapIndex];
          that.setData({
            battle_type_name: that.data.battle_type_list[Number(point)]
          })
          wx.setStorage({
            key: "battle_type",
            data: point
          })
        }
      }
    })
  },

   //对用户的输入进行验证
  checkInput:function(){
      var that       = this; 
      var error_info = ''; 
      if (this.data.event_stage == '') {
          error_info = '请输入比赛阶段';
      } else if (this.data.sum_frame == ''){
          error_info = '请输入总局数';
      } else if (this.data.player1_name == ''){
          error_info = '请输入红角选手姓名';
      } else if (this.data.player2_name == ''){
          error_info = '请输入蓝角选手姓名';
      } else if (this.data.event_stage.length>6){
          error_info = '比赛阶段字数不能大于6';
      } else if (isNaN(this.data.sum_frame)){
          error_info = "总局数只能为1-100的数字"
      } else if (Number(this.data.sum_frame)<1 || Number(this.data.sum_frame) > 100){
          error_info = "总局数只能为1-100的数字"
      }else{
        if (this.data.player2_name.indexOf(',') > 0) {

          var player2_name_array = this.data.player2_name.split(",");
          if (player2_name_array[0].length > 4 || player2_name_array[1].length > 4) {
            error_info = "蓝方选手姓名字数不能大于4"
          }
        } else if (this.data.player2_name.indexOf('，') > 0) {
          var player2_name_array = this.data.player2_name.split("，");
          if (player2_name_array[0].length > 4 || player2_name_array[1].length > 4) {
            error_info = "蓝方选手姓名字数不能大于4";
          }
        }

        if (this.data.player1_name.indexOf(',') > 0) {
          var player1_name_array = this.data.player1_name.split(",");
          if (player1_name_array[0].length > 4 || player1_name_array[1].length > 4) {
            error_info = "红方选手姓名字数不能大于4"
          }
        } else if (this.data.player1_name.indexOf('，') > 0) {
          var player1_name_array = this.data.player1_name.split("，");
          if (player1_name_array[0].length > 4 || player1_name_array[1].length > 4) {
            error_info = "红方选手姓名字数不能大于4";
          }
        }
          if(error_info == ''){
            if (this.data.player1_name.indexOf(',') < 0 && this.data.player1_name.indexOf('，') < 0 && this.data.player1_name.length > 4) {
               error_info = "红选手姓名字数不能大于4"
            } else if (this.data.player2_name.indexOf(',') < 0 && this.data.player2_name.indexOf('，') < 0 && this.data.player2_name.length > 4) {
               error_info = "蓝方选手姓名字数不能大于4"
            }
          }
       }
       if(error_info !=''){
           that.setData({
              item: {
                  display    :'show',
                  error_info :error_info
               }
            })
            that.closeErrorInfo();
            return false;
       }
       return true;
   },
   //定时器关闭提示信息
  closeErrorInfo:function(){
      var that = this;
      setTimeout(function(){that.setData({item: {display:'hide'}})},'2000')
  },

    //设置button的启用与失效
  setDisabled: function() {
      this.setData({button:{ disabled: !this.data.button.disabled}})
  }, 

    //保存对局的信息到本地缓存
  saveBattleInfo:function(battle_id){
      var that = this;
      var battle_type = wx.getStorageSync('battle_type');
      wx.setStorage({
          key:"battle_info",
          data:{
              battle_id     : battle_id,
              event_stage   : that.data.event_stage,
              sum_frame     : that.data.sum_frame,
              player1_name  : that.data.player1_name,
              player2_name  : that.data.player2_name,
              battle_type   : battle_type
          }
      });
      
      

      wx.setStorage({
          key: 'player1_score',
          data: battle_type == 2 ? '00':'000'
      });

      wx.setStorage({
          key: 'player2_score',
          data: battle_type == 2 ? '00':'000'
      });

      if(battle_type == 2){
        wx.removeStorageSync('snooker_battle_info');
      }
     
      //正在操作的选手默认为1，即红角
      wx.setStorage({
         key:'action_player',
         data:1
      })
    } 
})
